<?php

namespace services\statistics;

use Yii;
use common\components\Service;
use common\helpers\CommonHelper;
use common\models\clinic\Doctor;
use common\models\statistics\StatisticsDoctor;
use services\common\CommonService;

/**
 * Class StatisticsDoctorService
 * @package services\statistics
 */
class StatisticsDoctorService extends Service
{
    /**
     * 医生报表列表
     *
     * @param array $params
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public function getListPage(array $params = [], int $page = 0, int $pageSize = 0)
    {
        // 绑定项目列表
        $projectList = CommonService::getBindProjectList();

        $query = StatisticsDoctor::find()->alias('a')
            ->select([
                'a.*',
                'b.name doctor_name'
            ])
            ->innerJoin(Doctor::tableName().' b', 'b.id = a.clinic_doctor_id')
            ->where([
                'a.type' => $params['type'],
                'a.start_time' => $params['start_time'],
                'a.end_time' => $params['end_time'],
            ]);

        if (isset($params['project_id']) && $params['project_id']) {
            $query->andWhere(['a.project_id' => $params['project_id']]);
        } else {
            // 默认绑定的所有项目
            $query->andWhere(['a.project_id' => array_column($projectList, 'id')]);
        }

        // 排序
        if (isset($params['sort_column']) && $params['sort_column']) {
            $order["a.{$params['sort_column']}"] = $params['sort_type'] == 1 ? SORT_ASC : SORT_DESC;
        }
        $order['a.id'] = SORT_ASC;

        $data = $this->getListPageRes($query, $page, $pageSize, $order);

        $total = [
            'id' => 0,
            'project_id' => 0,
            'clinic_doctor_id' => 0,
            'clinic_id' => 0,
            'doctor_name' => '合计',
            'reg_num' => 0,
            'reg_money' => 0,
            'reg_avg_money' => 0,
            'loss_num' => 0,
            'loss_rate' => 0,
            'first_reg_num' => 0,
            'first_money' => 0,
            'first_avg_money' => 0,
            'again_reg_num' => 0,
            'again_money' => 0,
            'again_avg_money' => 0,
            'total_num' => 0,
            'total_money' => 0,
            'total_avg_money' => 0,
            'tmp_buy_clinic_num' => 0,
            'tmp_buy_patient_num' => 0,
            'tmp_buy_avg_num' => 0,
            'tmp_treament_clinic_num' => 0,
            'tmp_treament_patient_num' => 0,
            'tmp_treament_avg_num' => 0,
        ];

        // 格式化
        $list = [];
        foreach ($data['list'] as $row) {
            $list[] = [
                'id' => $row['id'],
                'project_id' => $row['project_id'],
                'clinic_doctor_id' => $row['clinic_doctor_id'],
                'clinic_id' => $row['clinic_id'],
                'doctor_name' => $row['doctor_name'],
                'reg_num' => $row['reg_num'],
                'reg_money' => $row['reg_money'],
                'reg_avg_money' => $row['reg_avg_money'],
                'loss_num' => $row['loss_num'],
                'loss_rate' => $row['loss_rate'],
                'first_reg_num' => $row['first_reg_num'],
                'first_money' => $row['first_money'],
                'first_avg_money' => $row['first_avg_money'],
                'again_reg_num' => $row['again_reg_num'],
                'again_money' => $row['again_money'],
                'again_avg_money' => $row['again_avg_money'],
                'total_num' => $row['total_num'],
                'total_money' => $row['total_money'],
                'total_avg_money' => $row['total_avg_money'],
                'tmp_buy_clinic_num' => $row['tmp_buy_clinic_num'],
                'tmp_buy_avg_num' => $row['tmp_buy_avg_num'],// 308人均购买
                'tmp_treament_avg_num' => $row['tmp_treament_avg_num'],// 308人均治疗【使用】
            ];

            $total['reg_num'] += $row['reg_num'];
            $total['reg_money'] += $row['reg_money'];
            $total['reg_avg_money'] = $total['reg_num'] > 0 ? number_format($total['reg_money'] / $total['reg_num'], 2, '.', '') : 0;
            $total['loss_num'] += $row['loss_num'];
            $total['loss_rate'] = $total['reg_num'] > 0 ? number_format($total['loss_num'] / $total['reg_num'], 2, '.', '') : 0;
            $total['first_reg_num'] += $row['first_reg_num'];
            $total['first_money'] += $row['first_money'];
            $total['first_avg_money'] = $total['first_reg_num'] > 0 ? number_format($total['first_money'] / $total['first_reg_num'], 2, '.', '') : 0;
            $total['again_reg_num'] += $row['again_reg_num'];
            $total['again_money'] += $row['again_money'];
            $total['again_avg_money'] = $total['again_reg_num'] > 0 ? number_format($total['again_money'] / $total['again_reg_num'], 2, '.', '') : 0;
            $total['total_num'] += $row['total_num'];
            $total['total_money'] += $row['total_money'];
            $total['total_avg_money'] = $total['total_num'] > 0 ? number_format($total['total_money'] / $total['total_num'], 2, '.', '') : 0;
            $total['tmp_buy_clinic_num'] += $row['tmp_buy_clinic_num'];
            $total['tmp_buy_patient_num'] += $row['tmp_buy_patient_num'];
            $total['tmp_buy_avg_num'] = $total['tmp_buy_patient_num'] > 0 ? number_format($total['tmp_buy_clinic_num'] / $total['tmp_buy_patient_num'], 2, '.', '') : 0;
            $total['tmp_treament_clinic_num'] += $row['tmp_treament_clinic_num'];
            $total['tmp_treament_patient_num'] += $row['tmp_treament_patient_num'];
            $total['tmp_treament_avg_num'] = $total['tmp_treament_patient_num'] > 0 ? number_format($total['tmp_treament_clinic_num'] / $total['tmp_treament_patient_num'], 2, '.', '') : 0;
        }
        foreach ($total as $key => $row) {
            if (in_array($key, [
                'reg_money',
                'reg_avg_money',
                'loss_rate',
                'first_money',
                'first_avg_money',
                'again_money',
                'again_avg_money',
                'total_money',
                'total_avg_money',
                'tmp_buy_avg_num',
                'tmp_treament_avg_num',
            ])) {
                $total[$key] = number_format($row, 2, '.', '');
            }
        }

        $data['list'] = $list;
        $data['total'] = $total;

        // 筛选条件
        $data['condition'] = [
            'project_list' => $projectList,
        ];

        return $data;
    }
}
